package ooseminar.length;

/**
 * Created by Abel Zhang.
 * User: twer
 * Date: Apr 18, 2011
 * Time: 6:12:29 PM
 */
public class Length {
    private LengthUnit unit;
    private double amount;

    public Length(LengthUnit unit, double amount) {
        this.unit = unit;
        this.amount = amount;
    }

    public Length add(Length length) {
        double amountToAdd = length.unit.convertTo(unit, length.amount);
        return new Length(unit, amount + amountToAdd);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Length length = (Length) o;
        if (unit == length.getUnit() && amount == length.getAmount())
            return true;

        if (unit != length.getUnit()) {
            return unit.convertTo(length.getUnit(), amount) == length.getAmount();
        }
        return false;
    }

    public double getAmount() {
        return amount;
    }

    public LengthUnit getUnit() {
        return unit;
    }

    @Override
    public String toString() {
        return String.format("%s, %s", unit, amount);
    }
}
